package com.lightheart.sphr.xiaobinggan.module.home.ui;

import android.content.Intent;
import android.graphics.Canvas;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemDragListener;
import com.chad.library.adapter.base.listener.OnItemSwipeListener;
import com.guwu.common.utils.ToastUtil;
import com.lightheart.sphr.xiaobinggan.R;
import com.lightheart.sphr.xiaobinggan.base.BaseActivity;
import com.lightheart.sphr.xiaobinggan.bean.Main6DataBean;
import com.lightheart.sphr.xiaobinggan.blackandwhite.ui.BlackAndWhiteActivity;
import com.lightheart.sphr.xiaobinggan.carousel.CarouselActivity;
import com.lightheart.sphr.xiaobinggan.customchannel.ChannelActivity;
import com.lightheart.sphr.xiaobinggan.customswitch.ui.SwitchActivity;
import com.lightheart.sphr.xiaobinggan.design.AutoNextActivity;
import com.lightheart.sphr.xiaobinggan.design.DesignActivity;
import com.lightheart.sphr.xiaobinggan.design.MaterAppBarActivity;
import com.lightheart.sphr.xiaobinggan.design.MateriaDesignActivity;
import com.lightheart.sphr.xiaobinggan.design.NestedScrollViewActivity;
import com.lightheart.sphr.xiaobinggan.design.RightAutoNextActivity;
import com.lightheart.sphr.xiaobinggan.design.RvAppBarActivity;
import com.lightheart.sphr.xiaobinggan.design.TopAppBarActivity;
import com.lightheart.sphr.xiaobinggan.design.WaveViewActivity;
import com.lightheart.sphr.xiaobinggan.electronic.ui.ElectronicActivity;
import com.lightheart.sphr.xiaobinggan.gps.GpsActivity;
import com.lightheart.sphr.xiaobinggan.kg.ui.KGActivity;
import com.lightheart.sphr.xiaobinggan.kg.ui.SideSlipActivity;
import com.lightheart.sphr.xiaobinggan.logo.LogoActivity;
import com.lightheart.sphr.xiaobinggan.module.home.adapter.HodgepodgeAdapter;
import com.lightheart.sphr.xiaobinggan.module.main.ui.MainActivity;
import com.lightheart.sphr.xiaobinggan.navigation.ui.ReadableBottomBarActivity;
import com.lightheart.sphr.xiaobinggan.popupWindow.PopupWindowActivity;
import com.lightheart.sphr.xiaobinggan.recyclerview.BaseRecyclerViewActivity;
import com.lightheart.sphr.xiaobinggan.scale.ui.ScaleActivity;
import com.lightheart.sphr.xiaobinggan.shadow.ShadowActivity;
import com.lightheart.sphr.xiaobinggan.stickynote.ui.StickyNoteActivity;
import com.lightheart.sphr.xiaobinggan.timeselection.ui.TimeIntegralActivity;
import com.lightheart.sphr.xiaobinggan.timeselection.ui.TimeSelectionActivity;
import com.lightheart.sphr.xiaobinggan.timeselection.ui.TimerActivity;
import com.lightheart.sphr.xiaobinggan.titlebar.ui.TitleBarActivity;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * AndroidStudio好看的插件:
 * (代码显示):Rainbow Brackets(运进度条)Nyan Progress
 * https://juejin.cn/post/6854573213104472071
 * 这15个Android开源库，只有经常逛Github的才知道！
 * https://juejin.cn/post/6856637313964441614
 * https://juejin.cn/user/1204720443862887
 * 尚硅谷java基础到实战
 * http://www.atguigu.com/download.shtml#JavaSE
 * 接口在线请求测试
 * https://getman.cn/?s=6fd375ea073c23212ba13233d25584d0
 * 最全android Demo
 * https://blog.csdn.net/qq_32666413/article/details/80534457
 * MotionLayout 示例
 * https://developer.android.google.cn/training/constraint-layout/motionlayout/examples
 * 动画
 * https://blog.csdn.net/harvic880925/article/details/50995268
 * //OkHttp 实现的抓包工具
 * https://github.com/DingProg/NetworkCaptureSelf
 * //视频播放器的框架
 * https://github.com/yangchong211/YCVideoPlayer
 * //Toast吐司工具类
 * https://github.com/getActivity/ToastUtils
 * 修改包名 一定要修改build和AndroidManifest 里面的：applicationId
 * https://cloud.tencent.com/developer/article/1036733
 * 万能TextView
 * https://github.com/lygttpod/SuperTextView
 * jetpackMvmm(玩转Android  kotlin +MvvM)
 * https://github.com/hegaojian/JetpackMvvm
 * 底部导航
 * https://github.com/iammert/ReadableBottomBar
 * https://github.com/oneHamidreza/MeowBottomNavigation
 * 兼容ViewPager与ViewPager2，支持多种指示器样式以及多种滑动样式
 * https://www.wanandroid.com/blog/show/2745
 * https://gitee.com/zziafyc/BannerViewPager-zhpanvip
 * :fire::fire::fire:崩溃日志手机端显示 ，测试妹妹的最爱，开发哥哥的小棉袄
 * https://www.wanandroid.com/blog/show/2739
 * 使用CoordinatorLayout、AppBarLayout和NestedScrollView实现简单的滑动固定标题栏
 * https://www.cnblogs.com/Mr-Deng/p/11758543.html
 * Android富文本开发，从0到1！
 * https://juejin.cn/user/1978776659695784 博客
 * https://github.com/yangchong211/YCCustomText.git
 * 图片选择库
 * https://github.com/LuckSiege/PictureSelector/blob/master/README_CN.md
 * 能否让APP永不崩溃？ 小光和我的对决
 * https://github.com/android-notes/Cockroach
 * https://juejin.cn/post/6901682664617705485
 * https://www.jianshu.com/p/abaff702f357
 * https://www.wanandroid.com/wenda/show/13485
 * Android ui调试工具
 * https://github.com/android-notes/SwissArmyKnife
 * android buildSrc管理依赖
 * https://blog.csdn.net/qq_23992393/article/details/109305122
 * https://www.yuque.com/guoquanliu/big9q9/wqgznm
 * https://handstandsam.com/2018/02/11/kotlin-buildsrc-for-better-gradle-dependency-management/
 * 最好用的 Android 状态栏导航栏库，没有之一
 * https://github.com/Zackratos/UltimateBarX
 * https://juejin.cn/post/6903165109485436935
 * 随机跳动的闪屏Logo标题-AnimLogoView
 * https://www.jianshu.com/p/629d4056f6eb
 * //Android 飘落动画 雪花动画 坠落动画 下降动画
 * https://github.com/gnkhandsome/RainView
 * //仿微信朋友圈查看图片下拽返回。集成轻松
 * https://blog.csdn.net/Mr___Xu/article/details/81452859
 * https://github.com/iielse/imageviewer
 * //仿今日头条自定义频道
 * https://github.com/chengzhicao/ChannelView
 * implementation 'com.github.chengzhicao:ChannelView:v1.1.2'
 * //解决 数据不足一屏 BottomSheetDialog 无法全屏展示
 * https://blog.csdn.net/bb199191/article/details/80928437
 * BaseItemDraggableAdapter 拖拽item
 * https://blog.csdn.net/u012055921/article/details/60876709
 **/
public class HodgepodgeActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.main_list)
    RecyclerView mainList;
    @BindView(R.id.title_name)
    TextView titleName;
    @BindView(R.id.root_layout)
    CoordinatorLayout rootLayout;

    private List<Main6DataBean> data;
    private HodgepodgeAdapter mainAdapter;
    private long mBackPressed;
    private static final int TIME_INTERVAL = 2000;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_hodgepodge;
    }

    @Override
    protected void initInjector() {
    }

    @Override
    protected void initView() {
        titleName.setText("一块小饼干而已");
        data = new ArrayList<>();
        initData();
        mainAdapter = new HodgepodgeAdapter(R.layout.main_item);
        mainAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        mainAdapter.setOnItemClickListener(this);
        mainList.setAdapter(mainAdapter);
        mainAdapter.setNewData(data);

        ItemDragAndSwipeCallback itemDragAndSwipeCallback = new ItemDragAndSwipeCallback(mainAdapter);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback);
        itemTouchHelper.attachToRecyclerView(mainList);
        // 开启拖拽
        mainAdapter.enableDragItem(itemTouchHelper, R.id.view_list_item_ll, true);
        mainAdapter.setOnItemDragListener(onItemDragListener);

        // 开启滑动删除
        mainAdapter.enableSwipeItem();
        mainAdapter.setOnItemSwipeListener(onItemSwipeListener);

    }

    private void initData() {
//        data.add(new Main6DataBean(R.mipmap.ic_launcher, "主页", MainActivity.class));
//        data.add(new Main6DataBean(R.mipmap.ic_launcher, "uCrop头像裁剪和根据时间来确定星座", Main3Activity.class));
//        data.add(new Main6DataBean(R.mipmap.ic_launcher, "仿ios加载框", Main4Activity.class));
//        data.add(new Main6DataBean(R.mipmap.ic_launcher, "微信发朋友圈拖动删", Main5Activity.class));
//        data.add(new Main6DataBean(R.mipmap.ic_launcher, "地址选择器", Main7Activity.class));
//        data.add(new Main6DataBean(R.mipmap.ic_launcher, "仿支付宝首页顶部伸缩滑动/中间层下拉刷新", Main8Activity.class));
//        data.add(new Main6DataBean(R.mipmap.ic_launcher, "仿京东顶部渐变/自定义指示器/viewpager3D效果/瀑布流", Main9Activity.class));
//        data.add(new Main6DataBean(R.mipmap.ic_launcher, "仿微信朋友圈九张图片的显示", Main2Activity.class));
//        data.add(new Main6DataBean(R.mipmap.ic_launcher, "自定义扫一扫", Main11Activity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "Activity跳转动画", AnimationActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "Androdi水印", WatermarkActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "防BoSS直聘我的界面", BossActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "黑白屏模式", BlackAndWhiteActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "时间选择器", TimeSelectionActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "侧滑栏的效果", KGActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "双侧滑栏的效果", SideSlipActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "过渡动画的折叠收缩View", CarouselActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "仿微博个人主页折叠效果", MateriaDesignActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "高仿美团APP页面滑动标题栏渐变效果", DesignActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "UI之自定义Behavior实现AppBarLayout越界弹性效果", MaterAppBarActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "RecyclerView", RvAppBarActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "超长折叠", TopAppBarActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "自动换行布局", AutoNextActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "NestedScrollViewActivity", NestedScrollViewActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "右边开始自动换行布局", RightAutoNextActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "shape阴影", ShadowActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "PopupWindow", PopupWindowActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "TitleBar", TitleBarActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "便利贴撕页效果", StickyNoteActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "刻度尺控件", ScaleActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "像素电子时钟", ElectronicActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "自定义Switch", SwitchActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "小机器人随波浪一起摇摆", WaveViewActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "android骚气底部导航一", ReadableBottomBarActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "GPS定位是否打开", GpsActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "Android监听当前Activity屏幕的触摸点击事件", TimerActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "android计时得积分", TimeIntegralActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "随机跳动的闪屏Logo标题", LogoActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "fragment中监听dispatchTouchEvent事件", MainActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "仿今日头条自定义频道和解决数据不足一屏BottomSheetDialog 无法全屏展示", ChannelActivity.class));
        data.add(new Main6DataBean(R.mipmap.ic_launcher, "BaseItemDraggableAdapter实现拖拽", BaseRecyclerViewActivity.class));

        for (int i = 0; i < 50; i++) {
            data.add(new Main6DataBean(R.mipmap.home_more, "待续", Main11Activity.class));
        }
    }

    OnItemDragListener onItemDragListener = new OnItemDragListener() {
        @Override
        public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {
//            CustomToastUtils.INSTANCE.imgToast("你在拖拽第" + (pos + 1) + "个位置的item哦！", "666666");
            ToastUtil.snack(rootLayout, "你在拖拽第" + (pos + 1) + "个位置的item哦！");

        }

        @Override
        public void onItemDragMoving(RecyclerView.ViewHolder source, int from,
                                     RecyclerView.ViewHolder target, int to) {

        }

        @Override
        public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {
            ToastUtil.snack(rootLayout, "拖拽到了第" + (pos + 1) + "个位置哦！");

        }
    };


    OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() {
        @Override
        public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {
            ToastUtil.snack(rootLayout, "继续向左滑动即可删除第" + (pos + 1) + "个位置的item");
        }

        @Override
        public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {

        }

        @Override
        public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
            ToastUtil.snack(rootLayout, "删除了第" + (pos + 1) + "个位置的item哦");
        }

        @Override
        public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder,
                                      float dX, float dY, boolean isCurrentlyActive) {

        }
    };

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        startActivity(new Intent(this, data.get(position).getActivity()));
        overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);
    }

    @Override
    public void onBackPressed() {
        if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()) {
            super.onBackPressed();
            return;
        } else {
            ToastUtil.snack(rootLayout, "再按一次退出");
        }
        mBackPressed = System.currentTimeMillis();
    }

    /**
     * 获取map中第一个key值
     *
     * @param map 数据源
     * @return
     */
    private static String getKeyOrNull(Map<String, Object> map) {
        String obj = null;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            obj = entry.getKey();
            if (obj != null) {
                break;
            }
        }
        return obj;
    }

    /**
     * 获取map中第一个数据值
     *
     * @param map 数据源
     * @return
     */
    private static Object getFirstOrNull(Map<String, Object> map) {
        Object obj = null;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            obj = entry.getValue();
            if (obj != null) {
                break;
            }
        }
        return obj;
    }
}
